package com.efast.dmom.aps.datahandlers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.CustomerPart;
import com.efast.dmom.aps.dao.CustomerPartDao;
import com.efast.dmom.aps.entity.CustomerPartEntity;
import com.efast.dmom.aps.util.TimeUtil;

@Component
public class CustomerPartDataHandler {

	@Autowired
	private CustomerPartDao customerPartDao;

	public void save(CustomerPart obj) {
		if (obj.getInnerData() != null) {
			obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
			obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//			obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			obj.getInnerData().setRowStatus(1);
			obj.getInnerData().setRowTrxId(1);
			customerPartDao.save(obj.getInnerData());
		}
	}

	public void update(CustomerPart obj) {
		if (obj.getInnerData() != null) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		//	obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			customerPartDao.update(obj.getInnerData());
		}
	}

	public void delete(CustomerPart obj) {
		if(obj.getInnerData() != null){
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		//	obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			customerPartDao.delete(obj.getInnerData());
		}

	}

	public CustomerPart getCustomerPartByCustomerIdAndPartId(long customerId, long partId) {
		CustomerPart customerPart = new CustomerPart();
		customerPart.setInnerData(customerPartDao.getCustomerPartByCustomerIdAndPartId(customerId,partId));
		return customerPart;
	}

	public CustomerPart getByName(String name) {
		CustomerPart customerPart = new CustomerPart();
		customerPart.setInnerData(customerPartDao.getByName(name));
		return customerPart;
	}

	public CustomerPart getById(long id) {
		CustomerPart customerPart = new CustomerPart();
		customerPart.setInnerData(customerPartDao.getById(id));
		return customerPart;
	}

	public List<CustomerPart> getCustomerPartByCustomerId(long customerId) {
		List<CustomerPart> customerPartList = new ArrayList<CustomerPart>();
		List<CustomerPartEntity> entityList = customerPartDao.getCustomerPartByCustomerId(customerId);
		if (entityList != null && entityList.size() > 0) {
			for(CustomerPartEntity entityData : entityList) {
				CustomerPart customerPart = new CustomerPart();
				customerPart.setInnerData(entityData);
				customerPartList.add(customerPart);
			}
			return customerPartList;
		}
		return null;
	}

	public List<CustomerPart> getCustomerPartByPartId(long partId) {
		List<CustomerPart> customerPartList = new ArrayList<CustomerPart>();
		List<CustomerPartEntity> entityList = customerPartDao.getCustomerPartByPartId(partId);
		if (entityList != null && entityList.size() > 0) {
			for(CustomerPartEntity entityData : entityList) {
				CustomerPart customerPart = new CustomerPart();
				customerPart.setInnerData(entityData);
				customerPartList.add(customerPart);
			}
			return customerPartList;
		}
		return null;
	}

}
